using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LogAnalyzer.Grabbers.Dialogs
{
    public partial class FileGrabberCfg : Form
    {
        public FileGrabberCfg()
        {
            InitializeComponent();

            foreach (string name in Enum.GetNames(typeof(DataEncoding)))
            {
                cmbEncoding.Items.Add(name);
            }

            Text = Globalization.ResourceManager.Current.GetString("FILE_GRABBER_CONFIGURATION");
            grpSelectFileOrFolder.Text = Globalization.ResourceManager.Current.GetString("SELECT_FILE_OR_FOLDER");
            grpOtherOptions.Text = Globalization.ResourceManager.Current.GetString("OTHER_OPTIONS");
        }

        private void btnBrowseFile_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.AddExtension = false;
                ofd.CheckFileExists = true;
                ofd.Filter = 
                    Globalization.ResourceManager.Current.GetString("DLG_LOG_FILE_DESC")+
                    " (*.txt; *.log)|*.log;*.txt|" +
                    Globalization.ResourceManager.Current.GetString("DLG_ALL_FILES_DESC") +
                    " (*.*)|*.*";
                ofd.Multiselect = false;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    txtFile.Text = ofd.FileName;
                    txtFile.SelectionStart = txtFile.Text.Length;
                }
            }
        }
        private void btnBrowseFolder_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog fbd = new FolderBrowserDialog())
            {
                fbd.Description = Globalization.ResourceManager.Current.GetString("CHOOSE_FOLDER");
                fbd.ShowNewFolderButton = false;
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    txtFile.Text = fbd.SelectedPath;
                    txtFile.SelectionStart = txtFile.Text.Length;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(txtFile.Text) || System.IO.Directory.Exists(txtFile.Text))
            {
                DialogResult = DialogResult.OK;
            }
            else
                MessageBox.Show(Globalization.ResourceManager.Current.GetString("FILE_OR_FOLDER_NOT_EXISTS"),
                    Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        public string FileName
        {
            get { return txtFile.Text; }
            set { txtFile.Text = value; }
        }
        public long BufferSize
        {
            get 
            {
                return (long)(nupBufferKB.Value * 1024);
            }
            set 
            {
                if (value == 0)
                {
                    nupBufferKB.Value = 0;
                    return;
                }
                nupBufferKB.Value = (long)Math.Round((decimal)value / (decimal)1024, 0);
            }
        }
        public DataEncoding Encoding
        {
            get
            {
                return (DataEncoding)Enum.Parse(typeof(DataEncoding), cmbEncoding.SelectedItem.ToString());
            }
            set
            {
                string[] names = Enum.GetNames(typeof(DataEncoding));
                for (int i = 0; i < names.Length; i++)
                {
                    if (names[i].Equals(Enum.GetName(typeof(DataEncoding), value)))
                    {
                        cmbEncoding.SelectedIndex = i;
                        return;
                    }
                }
            }
        }


    }
}